import React from 'react';
import { connect } from 'dva';
import { Link, routerRedux } from 'dva/router';
import { WingBlank, WhiteSpace, List, Flex, Toast, TextareaItem, Button } from 'antd-mobile';
import { createForm } from 'rc-form';
import MemberInfo from 'components/MemberInfo';
import { PartumScaleReport } from 'components/scale';

const { Item, Item: { Brief } } = List;

const NativeScalePartumReportPage = ({ dispatch, location, form, ...rest }) => {
  const { getFieldProps, getFieldValue, getFieldError, validateFields } = form;
  const exitHandler = () => {
    // Toast.info('点击后退退出', 1);
    window.location = 'http://goBack';
  };

  const submitHandler = () => {

    validateFields((errors, value) => {
      if (errors) {
        const fieldNames = Object.keys(errors);
        if (fieldNames[0]) {
          return Toast.fail(getFieldError(fieldNames[0]));
        }
      }
      const comment = getFieldValue('comment');
      // value.programeint = rest.programeint.join(';');
      // value.programerec = rest.programerec.join(';');
      const { type, doctorid, patientid, patientvalue } = location.state;
      const { Report1, Report2, Report3, Programe } = rest.report;

      const payload = {
        type,
        doctorid,
        patientid,
        patientvalue,
        Report1: Report2,
        Report2: Report1,
        Report3: Report3,
        Programe: Programe,
        comment,
        // ...value,
      };
      dispatch({
        type: 'scale/submitScalePrograme',
        payload,
      });
    });

    Toast.success('保存绑定成功', 1);
    window.location = 'http://goBack';
  };

  return (
    <div>
      <MemberInfo {...location.state.member} />
      
      <WhiteSpace />
      <PartumScaleReport {...rest.report} />
      <WingBlank size="md">
        <List>
          <Item>
            调养方案：
            <Brief>
              <TextareaItem
                {...getFieldProps('Programe', {
                  initialValue: rest.report ? rest.report.Programe : '',
                })}
                rows={5}
                style={{ border: '1px solid #ddd' }}
              />
            </Brief>
          </Item>
        </List>
        <WhiteSpace size="xl" />
        <List>
          <Item>
            备注：
            <Brief>
              <TextareaItem
                {...getFieldProps('comment', {
                })}
                rows={5}
                style={{ border: '1px solid #ddd' }}
              />
            </Brief>
          </Item>
        </List>
        <List>
          <WhiteSpace size="xl" />
          <WingBlank>
            <Flex>
              <Flex.Item>
                <Button onClick={exitHandler}>退出</Button>
              </Flex.Item>
              <Flex.Item>
                <Button type="primary" onClick={submitHandler}>保存</Button>
              </Flex.Item>
            </Flex>
          </WingBlank>
          <WhiteSpace size="xl" />
        </List>
      </WingBlank>
    </div>
  );
};

export default connect(({ scale }) => ({ ...scale }))(createForm()(NativeScalePartumReportPage));
